﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MREO.data;
using System.Data.SqlServerCe;

namespace MREO.gui
{
    public partial class fLogOn : Form
    {
        public Users oUser;

        Connection Conn = new Connection();
        SqlCeDataAdapter adapter = new SqlCeDataAdapter();
        SqlCeCommand command = null;

        public fLogOn()
        {
            InitializeComponent();

            getUsers();

        }

        private void getUsers()
        {
            DataTable dt = new DataTable();

            try
            {
                Conn.con.Open();

                command = new SqlCeCommand("SELECT id, fam, im, otch, lvl" +
                                            " FROM users ORDER BY fam, im, otch"
                                            , Conn.con);
                adapter.SelectCommand = command;
                adapter.Fill(dt);
            }
            catch (Exception exc) { throw new Exception(exc.Message); }
            finally
            {
                command.Dispose();
                adapter.Dispose();
                if (Conn.con.State == ConnectionState.Open)
                    Conn.con.Close();
            }

            foreach (DataRow dr in dt.AsEnumerable())
            {
                oUser = new Users();
                oUser._id = (int)dr["id"];
                oUser._fam = dr["fam"].ToString();
                oUser._im = dr["im"].ToString();
                oUser._otch = dr["otch"].ToString();
                oUser._lvl = (int)dr["lvl"];

                comboBox_users.Items.Add(oUser);
                comboBox_users.ValueMember = "_id";
                comboBox_users.DisplayMember = "_fam";
            }
        }

        private void button_logon_Click(object sender, EventArgs e)
        {
            int intBuff = 0;
            try
            {
                Conn.con.Open();

                command = new SqlCeCommand("SELECT count(id)" +
                                            " FROM users WHERE id = @id AND pwd = @pwd"
                                            , Conn.con);
                command.Parameters.Add("@id", SqlDbType.Int).Value = ((Users)comboBox_users.SelectedItem)._id;
                command.Parameters.Add("@pwd", SqlDbType.NVarChar).Value = textBox_pwd.Text.Trim();
                intBuff = (int)command.ExecuteScalar();
            }
            catch (Exception exc) { MessageBox.Show(exc.Message); }
            finally
            {
                command.Dispose();
                adapter.Dispose();
                if (Conn.con.State == ConnectionState.Open)
                    Conn.con.Close();
            }

            if (intBuff != 1)
            {
                MessageBox.Show("Введенный пароль не верен.");
                return;
            }

            oUser = comboBox_users.SelectedItem as Users;

            DialogResult = DialogResult.OK;
        }

        private void fLogOn_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            button_logon_Click(sender, new EventArgs());
        }

    }
}
